var answerList = [];

//倒计时
function cutTime() {
    var maxtime = $('#maxtime').val() * 60;
    /*单位秒*/
    var loading2 = setInterval(function () {
        if (maxtime >= 0) {
            var minutes = Math.floor(maxtime / 60);
            var seconds = Math.floor(maxtime % 60);
            if (minutes >= 10) {
                minutes = minutes
            } else {
                minutes = '0' + minutes
            }
            if (seconds >= 10) {
                seconds = seconds
            } else {
                seconds = '0' + seconds
            }
            var msg;
            if (maxtime > 0) {
                msg = minutes + ":" + seconds;

            } else {
                msg = '倒计时已结束';
                saveAnswer();

            }
            $('.timeStr span').html(msg);
            // maxtime = maxtime

            --maxtime;
        } else {
            clearInterval(loading2)
        }
    }, 1000)
}

//词汇题\写作、翻译输入
function vocabulary(_this, id, trueAnswer) {
    let index = answerList.findIndex(item => {
        return item.id == id;
    });
    if (index > -1) {
        answerList[index].answer = _this.value
    } else {
        var ar = {
            id: id,
            answer: _this.value,
            trueAnswer: trueAnswer,
            type: $('#type').val(),
            remark: '',
        }
        answerList.push(ar)
    }
    console.log('answerList', answerList)
    console.log('vocabulary', _this.value)
}

//单选选择题选择
function chooseOption(_this, id, userAnswer, trueAnswer) {
    $(_this).children('span').addClass('on');
    $(_this).siblings('li').children('span').removeClass('on');
    let index = answerList.findIndex(item => {
        return item.id == id;
    });
    if (index > -1) {
        answerList[index].answer = userAnswer;
    } else {
        var ar = {
            id: id,
            answer: userAnswer,
            trueAnswer: trueAnswer,
            type: $('#type').val(),
            remark: '',
        }
        answerList.push(ar)
    }
}

//多选选择题选择
function chooseOption2(_this, id, userAnswer, trueAnswer) {
    $(_this).children('span').addClass('on');
    let index = answerList.findIndex(item => {
        return item.id == id;
    });
    if (index > -1) {
        if (answerList[index].answer.indexOf(userAnswer) > -1) {
            $(_this).children('span').removeClass('on');
            answerList[index].answer = answerList[index].answer.replace(new RegExp(userAnswer, 'g'), '');
        } else {
            answerList[index].answer = answerList[index].answer + userAnswer;

        }
    } else {
        var ar = {
            id: id,
            answer: userAnswer,
            trueAnswer: trueAnswer,
            type: $('#type').val(),
            remark: '',
        }
        answerList.push(ar)
    }
    console.log('answerList', answerList)
}

//翻译题
function interpret(_this, id, trueAnswer, type) {
    let index = answerList.findIndex(item => {
        return item.id == id;
    });
    if (index > -1) {
        if (type == '1') {
            //    输入为翻译answer
            answerList[index].answer = _this.value
        } else {
            //   输入为倒装 remark
            answerList[index].remark = _this.value
        }
    } else {
        if (type == '1') {
            var ar = {
                id: id,
                answer: _this.value,
                trueAnswer: trueAnswer,
                type: $('#type').val(),
                remark: '',
            }
        } else {
            var ar = {
                id: id,
                answer: "",
                trueAnswer: trueAnswer,
                type: $('#type').val(),
                remark: _this.value,
            }
        }
        answerList.push(ar)
    }
    console.log('answerList', answerList)

}

//保存答案
function saveAnswer() {
    var endTime = Math.ceil(new Date().getTime() / 1000);
    var duration = Number(endTime - $('#startTime').val());
    console.log('duration', duration)
    var type = $('#type').val();
    console.log('type', type)

    if (type == 9 || type == 8) {
        console.log('$(\'#upUrl\').val()', $('#upUrl').val())
        if ($('#upUrl').val() != '') {
            var ar = {
                id: $('#id').val(),
                answer: $('#upUrl').val(),
                trueAnswer: $('#answer').val(),
                type: $('#type').val(),
                remark: '',
            }
            answerList.push(ar)
        } else {
            $('.question_modal .modal-body p').html('要先上传录音才能继续下一题哦')
        }


    }
    console.log('answerList', answerList)
    if (answerList.length == 0 && duration < $('#maxtime').val() * 60) {
        $('.question_modal').fadeIn()

    } else {

        $.ajax({
            type: 'POST',
            cache: false,
            url: "/cn/evaluation/save-answer",
            data: {
                pubId: $("#pubId").val(),
                time: duration,
                subjectId: $('#subjectId').val(),
                content: JSON.stringify(answerList),
            },
            dataType: "json",
            success: function (res) {
                if (res.code == 1) {
                    var next = $("#nextSubjectId").val()
                    answerList = []
                    if ($('#pageTotal').val() > $('#page').val()) {
                        window.location.href = "/evaluation/question.html?subjectId=" + $("#subjectId").val() + "&pubId=" + $("#pubId").val() + "&page=" + Number(Number($('#page').val()) + 1)
                    } else {
                        if (next != 0) {
                            window.location.href = "/evaluation/question.html?subjectId=" + $("#nextSubjectId").val() + "&pubId=" + $("#pubId").val()
                        } else {
                            window.location.href = "/evaluation/result.html?pubId=" + $("#pubId").val()
                        }
                    }

                } else {
                    alert(res.message)
                }
            }
        })
    }

}

//听力播放
var audio = document.getElementById("listenAudio")

function playAudio(_this) {
    audio.play();
    $('.play').hide()
    $('.pause').show()
    audio.addEventListener("timeupdate", function (event) {
        var totalTime = formateSeconds(audio.duration);
        var currentTime = formateSeconds(audio.currentTime);
        var pro = Number(Number(audio.currentTime / audio.duration) * 100).toFixed(2);

        $('.audioTime span:first-child').html(currentTime)
        $('.audioTime span:last-child').html(totalTime)
        $('.autioPro span').css({"width": pro + '%'})

    })

}


function aduioChange(_this) {
    var cur = $(_this).val();
    console.log('cur', cur);
    var num = cur + '%';
    audio.currentTime = audio.duration * num;
    audio.play();
}

function pauseAudio() {
    audio.pause();
    $('.play').show()
    $('.pause').hide()
}

function formateSeconds(endTime) {
    let secondTime = parseInt(endTime);//将传入的秒的值转化为Number
    let min = 0;// 初始化分
    let h = 0;// 初始化小时
    let result = '';
    if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
        min = parseInt(secondTime / 60);//获取分钟，除以60取整数，得到整数分钟
        secondTime = parseInt(secondTime % 60);//获取秒数，秒数取佘，得到整数秒数
        if (min > 60) {//如果分钟大于60，将分钟转换成小时
            h = parseInt(min / 60);//获取小时，获取分钟除以60，得到整数小时
            min = parseInt(min % 60);//获取小时后取佘的分，获取分钟除以60取佘的分
        }
    }
    result = `${h.toString().padStart(2, '00')}:${min.toString().padStart(2, '00')}:${secondTime.toString().padStart(2, '00')}`;
    return result;
}

//quite
function quite() {
    $('.Quite_modal').fadeIn()
}

function closeModal(_this) {
    $(_this).parents('.next_modal').fadeOut()
}

function pageBack() {
    window.location.href = "/cn/evaluation/get-paper"
}
